import {axios} from '@/utils/request'
import qs from 'qs'
import Vue from "vue";

// 文件下载
function fileDownload(data, fileName) {

    var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
    var contentDisposition = decodeURI(data.headers['content-disposition']);
    var result = patt.exec(contentDisposition);
    try {
        fileName = result[1].replace(/\"/g, '');
    } catch (error) {
        // 错误处理，可能需要记录错误或通知用户
    }
    if (!fileName) {
        return;
    }

    // 创建一个 Blob 对象
    let arr = [];
    if (data.data) arr.push(data.data)

    var blob = new Blob(arr, {type: 'application/octet-stream'});

    // IE 专用下载方法
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else {
        // 对于其他浏览器，创建一个链接并模拟点击进行下载
        var url = window.URL.createObjectURL(blob);
        var downloadLink = document.createElement('a');
        downloadLink.href = url;
        downloadLink.download = fileName;

        document.body.appendChild(downloadLink);
        downloadLink.click();
        document.body.removeChild(downloadLink);

        window.URL.revokeObjectURL(url);  // 清除创建的对象URL
    }
}

export function downFilePostJSON(url, parameter, method = 'post') {
    return axios({
        url: url,
        data: parameter,
        method,
        responseType: 'blob',
    })
}
export function downFilePost(url, parameter, method = 'post') {
    return axios({
        url: url,
        data: parameter,
        method,
        responseType: 'blob',

    })
}
export function downFilePostUrl(url, parameter, method = 'post') {
    return axios({
        url: url,
        data:parameter,
        method,
        headers: {            'content-type': 'multipart/form-data',  },
        // headers: {'content-type': 'application/x-www-form-urlencoded'},
    })
}
export function downFileGet(url, parameter, method = 'get') {
    return axios({
        url: url,
        params:parameter,
        method,
        responseType: 'blob',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
    })
}

/**
 * 下载文件请求
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url, parameter, method = 'post') {
    return axios({
        url: url,
        data: qs.stringify(parameter),
        method,
        responseType: 'blob',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
    })
}


export function exportDetailList(parameter, method) {
    return downFile('/vatoutinv/vmsClassifyCode/exportDetailList', parameter, method).then((data) => {
        console.log(data)
        var fileName = ''
        fileDownload(data, fileName)
    })
}


export function exportVmsAvalibleTaxRateLogDetail(parameter, method) {
    return downFile('/vatoutinv/vmsAvalibleTaxRate/exportVmsAvalibleTaxRateLogDetail', parameter, method).then((data) => {
        console.log(data)
        var fileName = ''
        fileDownload(data, fileName)
    })
}

export function exportVmsAvalibleTaxRate(parameter, method) {
    return downFile('/vatoutinv/vmsAvalibleTaxRate/exportVmsAvalibleTaxRate', parameter, method).then((data) => {
        console.log(data)
        var fileName = ''
        fileDownload(data, fileName)
    })
}

export function downloadExcel(parameter, method) {
    return downFile('/vatoutinv/vmsManualInvoicing/downloadExcel', parameter, method).then((data) => {
        var fileName = ''
        fileDownload(data, fileName = '')
    })
}


export function vmsFullOutInvoiceFileDownload(parameter, method) {
    return downFilePost('/vatoutinv/vmsFullOutInvoice/fileDownload', parameter, method).then((data) => {
        var fileName = ''
        fileDownload(data, fileName = '')
    }).catch(err => {
        console.log("下载发票失败，错误信息：" + err);
    })
}

export function exportCode(parameter, method) {
    let params = '';
    if (parameter.sphfwssflhbbm) {
        params += `sphfwssflhbbm=${parameter.sphfwssflhbbm}&`
    }
    if (parameter.sphfwmc) {
        params += `sphfwmc=${parameter.sphfwmc}&`
    }
    if (parameter.sphfwfljc) {
        params += `sphfwfljc=${parameter.sphfwfljc}&`
    }
    return downFilePost('/vatoutinv/vmsClassifyCode/exportCode', parameter, method).then((data) => {
        var fileName = ''
        fileDownload(data, fileName = '')
    })
}

export function downloadExcelCommon(parameter, method) {
    return downFileGet('/vatoutinv/common/downloadExcel', parameter, method).then((data) => {
        console.log(data)
        var fileName = ''
        fileDownload(data, fileName)
    })
}